<?php

namespace Hilaoyu\LaravelExtend\Services\Files\Controllers;

use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelPrivateApi\PrivateApiControllerAbstract;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;


class FilesServiceController extends PrivateApiControllerAbstract
{

    use FilesControllerHelperTrait;

    public function __construct()
    {
        UtilSupport::tryQuiet(function (){
           parent::__construct();
        });

    }

    private function checkAllowService($disk){
        if(!FileService::getDiskConfig($disk,'provide_service')){
            abort(404);
        }
    }

    public function uploader(Request $request,$disk){
        $this->checkAllowService($disk);
        return $this->checkAllowUpload($request,$disk)
            ->then(function ($request) use ($disk){
                $encrypter = $this->getEncryptor();
                return comResponse()->success()
                    ->with(route('api.l-e-s.files.upload', ['disk' => $disk]),null,$encrypter)
                    ->json();

            });
    }
    public function uploadTaskCreate(Request $request,$disk){
        $this->checkAllowService($disk);
        return $this->checkAllowUpload($request,$disk)
            ->then(function ($request) use ($disk){
                $encrypter = $this->getEncryptor();
                $uoploadController = new FileUploadController();
                return $uoploadController->uploadTaskCreate($request,$disk,$encrypter);

            });
    }
    public function uploadTaskGet(Request $request){
        $id = $this->getRequestData('id','');
        if(!$id || ! $filesModel = FileService::loadFilesModel($id)){
            abort(404);
        }
        $disk = $filesModel->getAttribute('disk');
        $this->checkAllowService($disk);
        return $this->checkAllowUpload($request,$disk)
            ->then(function ($request) use ($filesModel){
                $encrypter = $this->getEncryptor();
                $uoploadController = new FileUploadController();
                $filesModel->setUrlAbsolute(true);
                return $uoploadController->uploadTaskGet($request,$filesModel,$encrypter);

            });
    }


    public function useFor(Request $request){
        $id = $this->getRequestData('id','');
        if(!$id || ! $filesModel = FileService::loadFilesModel($id)){
            abort(404);
        }
        $disk = $filesModel->getAttribute('disk');
        $this->checkAllowService($disk);

        $FS = new FileService('','',$filesModel);
        $app = $request->route('_auth_app');
        $FS->filesModelFill(['app_id'=>data_get($app,'id')]);

        $FS = $FS->queueUseFor();
        $encrypter = $this->getEncryptor();
        $filesModel = $FS->filesModel();
        if($filesModel){
            $filesModel->setUrlAbsolute(true);
        }
        return comResponse()->success()
            ->with($filesModel,null,$encrypter)
            ->json();
    }
    public function url(Request $request,$disk=''){
        $id = $this->getRequestData('id','');
        $path = $this->getRequestData('path','');
        $filesModel = null;
        if($id){
            $filesModel = FileService::loadFilesModel($id);
            if($filesModel){
                $disk = '';
                $path = '';
            }
        }
        $FS = new FileService($disk,$path,$filesModel);
        $encrypter = $this->getEncryptor();
        return comResponse()->success()
            ->with($FS->url(true),null,$encrypter)
            ->json();
    }
    public function tempUrl(Request $request,$disk=''){
        $id = $this->getRequestData('id','');
        $path = $this->getRequestData('path','');
        $time = $this->getRequestData('ttl','');
        $ttl = null;
        if($time){
            $ttl = UtilSupport::tryQuiet(function () use ($time){
                return Carbon::createFromTimeString($time);
            });
        }
        if(!$ttl){
            $temp_url_expire_time = FileService::getDiskConfig($disk,'temp_url_expire_time');
            if($temp_url_expire_time <= 0){
                $temp_url_expire_time = 60;
            }
            $ttl = Carbon::now()->addMinutes($temp_url_expire_time);
        }
        $filesModel = null;
        if($id){
            $filesModel = FileService::loadFilesModel($id);
            if($filesModel){
                $disk = '';
                $path = '';
            }
        }
        $FS = new FileService($disk,$path,$filesModel);
        $encrypter = $this->getEncryptor();
        return comResponse()->success()
            ->with($FS->tempUrl($ttl,true),null,$encrypter) 
            ->json();
    }


    public function fileInfo(Request $request,$disk=''){
        //todo
        $id = $this->getRequestData('id','');
        $path = $this->getRequestData('path','');
        $filesModel = null;
        if($id ){
            $filesModel = FileService::loadFilesModel($id);
        }
        $Fs = new FileService($disk,$path,$filesModel);

        $filesModel = $Fs->filesModel();
        $filesModel->setUrlAbsolute(true);
        $encrypter = $this->getEncryptor();
        return comResponse()->success()->with($filesModel,null,$encrypter)->json();
    }

}
